package com.example.hehecat.yuejian1.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.ActivityCollectContract;
import com.example.hehecat.yuejian1.mvp.model.bean.ActivityBean;
import com.example.hehecat.yuejian1.mvp.presenter.ActivityCollectPresenter;
import com.example.hehecat.yuejian1.ui.adapter.ActivityListAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;
import com.example.hehecat.yuejian1.ui.wiget.MyStaticVariable;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.content.ContentValues.TAG;

public class AcitvityCollectFragment extends Fragment implements ActivityCollectContract.View {

    Unbinder unbinder;
    @BindView(R.id.activity_collect)
    RecyclerView activityCollect;


    private ActivityListAdapter adapter;
    private ActivityCollectPresenter presenter;

    int user_id;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activity_collect, container, false);
        unbinder = ButterKnife.bind(this,view);
        user_id= MyStaticVariable.newInstance().getUserid();

        //创建布局管理
        final RecyclerView recyclerView = view.findViewById(R.id.activity_collect);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new MyItemDecoration());

        //创建适配器
        adapter = new ActivityListAdapter(R.layout.item_list,getContext());
        //监听item点击
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter madapter, View view, int position) {
                moveToActivityInfo(Objects.requireNonNull(adapter.getItem(position)).getActivityid());
                Log.d(TAG,"onItemClick: "+adapter.getItem(position).getActivityname());

            }
        });
        //给RecyclerView设置适配器
        presenter = new ActivityCollectPresenter(this);
        presenter.updateAdapterData(user_id);
        recyclerView.setAdapter(adapter);
        return view;
    }
    public static AcitvityCollectFragment newInstance(String name){
        AcitvityCollectFragment fragment = new AcitvityCollectFragment();
        Bundle args = new Bundle();
        args.putString("name",name);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void updateAdapterData(List<ActivityBean> data) {
        adapter.addData(data);
    }

    @Override
    public void moveToActivityInfo(int activity_id) {
        ActivityInfoFragment activityInfoFragment = ActivityInfoFragment.newInstance(activity_id);
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content,activityInfoFragment,null)
                .commit();
    }

    @OnClick(R.id.activity_return)
    void activityCollectReturn(){
        MineFragment fragment = new MineFragment();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content,fragment,null)
                .commit();
    }
}
